<?php
class FaceService {
	const SUCCESS = 'success';
	const ERROR = 'error';
	//0审核中 1不通过 
	//存储头像的路径
	private $_savefacePath = "/static/attached/face/";
	private $_status = array (0 => "等待审核", 1 => "通过", - 1 => "不通过" );
	
	public function __construct() {
	
	}
	
	public function add($uid, $face, $username) {
		if ($this->checkUser ( $uid )) {
			$this->addUser ( $face, $uid, $username );
		} else {
			$this->saveUser ( $face, $uid, $username );
		}
	}
	
	private function saveUser($face, $uid, $username) {
		return FaceModel::instance ()->saveFaceByUid ( array ('face' => $face, 'username' => $username, 'status' => 0 ), $uid );
	}
	
	private function addUser($face, $uid, $username) {
		return FaceModel::instance ()->addFaceList ( array ('face' => $face, 'uid' => $uid, 'username' => $username, 'status' => 0 ) );
	}
	
	/**
	 * 检查申请队列是否已经存在
	 * @param int $uid
	 * @return true 不存在 false 存在
	 */
	private function checkUser($uid) {
		$user = FaceModel::instance ()->getFaceByUid ( $uid );
		return empty ( $user ) ? true : false;
	}
	
	/**
	 * 验证头像
	 * @param int $id
	 * @param String $type
	 */
	public function validFace($id, $type) {
		$face = FaceModel::instance ()->getFaceById ( $id );
		if ($type == 'pass') {
			//删除申请
			FaceModel::instance ()->saveFaceByid ( array ('status' => 1 ), $id );
			return $face;
		} else {
			FaceModel::instance ()->saveFaceByid ( array ('status' => - 1 ), $id );
			return self::ERROR;
		}
	}
	
	public function delFaceById($id) {
		FaceModel::instance ()->delFaceById ( $id );
	}
	/**
	 * 获取所有提交的头像审核
	 */
	public function getVaildFaceList() {
		return FaceModel::instance ()->getFaceList ();
	}
	//上传新的图片
	public function upload($uid) {
	
		if (! empty ( $_FILES )) {
			$ext = pathinfo ( $_FILES ['Filedata'] ['name'] );
			$ext = strtolower ( $ext ['extension'] );
			$tempFile = $_FILES ['Filedata'] ['tmp_name'];
			$dir = date ( "Ym", time () );
			$targetPath = getcwd () . $this->_savefacePath . $dir . '/';
			if (! is_dir ( $targetPath )) {
				mkdir ( $targetPath, 0777, true );
			}

			$new_file_name = $uid . '_source.' . $ext;
			$targetFile = $targetPath . $new_file_name;
			move_uploaded_file ( $tempFile, $targetFile );
			if (! file_exists ( $targetFile )) {
				$ret ['result_code'] = 0;
				$ret ['result_des'] = '上传失败';
			} elseif (! $imginfo = $this->getImageInfo ( $targetFile )) {
				$ret ['result_code'] = 101;
				$ret ['result_des'] = '文件不存在';
			} else {
				$img = $this->_savefacePath . $dir . '/' . $new_file_name;
				$this->resize ( $img );
				$ret ['result_code'] = 1;
				$ret ['result_des'] = $img;
			}
		} else {
			$ret ['result_code'] = 100;
			$ret ['result_des'] = 'No File Given';
		}
		return $ret;
	}
	
	//剪裁之后的图片
	public function thumb() {
		if (! $image = $_POST ["img"]) {
			$ret ['result_code'] = 101;
			$ret ['result_des'] = "图片不存在";
		} else {
			$urlImage = $image;
			$image = getcwd () . $image;
			$info = $this->getImageInfo ( $image );
			if (! $info) {
				$ret ['result_code'] = 102;
				$ret ['result_des'] = "图片不存在";
			} else {
				$x = $_POST ["x"];
				$y = $_POST ["y"];
				$w = $_POST ["w"];
				$h = $_POST ["h"];
				$width = $srcWidth = $info ['width'];
				$height = $srcHeight = $info ['height'];
				$type = empty ( $type ) ? $info ['type'] : $type;
				$type = strtolower ( $type );
				unset ( $info );
				// 载入原图
				$createFun = 'imagecreatefrom' . ($type == 'jpg' ? 'jpeg' : $type);
				$srcImg = $createFun ( $image );
				//创建缩略图
				if ($type != 'gif' && function_exists ( 'imagecreatetruecolor' ))
					$thumbImg = imagecreatetruecolor ( $width, $height );
				else
					$thumbImg = imagecreate ( $width, $height );
				
		// 复制图片
				if (function_exists ( "imagecopyresampled" ))
					imagecopyresampled ( $thumbImg, $srcImg, 0, 0, 0, 0, $width, $height, $srcWidth, $srcHeight );
				else
					imagecopyresized ( $thumbImg, $srcImg, 0, 0, 0, 0, $width, $height, $srcWidth, $srcHeight );
				if ('gif' == $type || 'png' == $type) {
					
					$background_color = imagecolorallocate ( $thumbImg, 0, 255, 0 );
					imagecolortransparent ( $thumbImg, $background_color );
				}
				// 对jpeg图形设置隔行扫描
				if ('jpg' == $type || 'jpeg' == $type)
					imageinterlace ( $thumbImg, 1 );
				
		// 生成图片
				$imageFun = 'image' . ($type == 'jpg' ? 'jpeg' : $type);
				$imageFun ( $thumbImg, $image, 100 );
				$thumbImg01 = imagecreatetruecolor ( 200, 200 );
				imagecopyresampled ( $thumbImg01, $thumbImg, 0, 0, $x, $y, 200, 200, $w, $h );
				
				$imageFun ( $thumbImg01, $image, 100 );
				imagedestroy ( $thumbImg01 );
				
				imagedestroy ( $thumbImg );
				imagedestroy ( $srcImg );
				$ret ['result_code'] = 1;
				$ret ['result_des'] = array ("big" => $urlImage );
			}
		}
		return $ret;
	}
	
	function resize($ori) {
		if (preg_match ( '/^http:\/\/[a-zA-Z0-9]+/', $ori )) {
			return $ori;
		}
		$info = $this->getImageInfo ( getcwd () . $ori );
		if ($info) {
			//上传图片后切割的最大宽度和高度
			$width = 500;
			$height = 500;
			$scrimg = getcwd () . $ori;
			if ($info ['type'] == 'jpg' || $info ['type'] == 'jpeg') {
				$im = imagecreatefromjpeg ( $scrimg );
			}
			if ($info ['type'] == 'gif') {
				$im = imagecreatefromgif ( $scrimg );
			}
			if ($info ['type'] == 'png') {
				$im = imagecreatefrompng ( $scrimg );
			}
			if ($info ['width'] <= $width && $info ['height'] <= $height) {
				return;
			} else {
				if ($info ['width'] > $info ['height']) {
					$height = intval ( $info ['height'] / ($info ['width'] / $width) );
				} else {
					$width = intval ( $info ['width'] / ($info ['height'] / $height) );
				}
			}
			$newimg = imagecreatetruecolor ( $width, $height );
			imagecopyresampled ( $newimg, $im, 0, 0, 0, 0, $width, $height, $info ['width'], $info ['height'] );
			imagejpeg ( $newimg, getcwd () . $ori );
			imagedestroy ( $im );
		}
		return;
	}
	
	public function getImageInfo($img) {
		$imageInfo = getimagesize ( $img );
		if ($imageInfo !== false) {
			$imageType = strtolower ( substr ( image_type_to_extension ( $imageInfo [2] ), 1 ) );
			$info = array ("width" => $imageInfo [0], "height" => $imageInfo [1], "type" => $imageType, "mime" => $imageInfo ['mime'] );
			return $info;
		} else {
			return false;
		}
	}

}